﻿@{
    ViewBag.Title = "OrderCashIndex";
    Layout = "~/Views/Shared/_Index.cshtml";
}
@section Content{
    <style>
        .btn-default .badge-danger {
            background-color: red;
        }

        .topPanel {
            height: 95px;
        }

            .topPanel .search {
                padding-top: 10px;
            }

        .zdyUL {
            list-style-type: none;
            margin: 5px 0px 0px -3px;
            padding: 0px;
        }

            .zdyUL li {
                float: left;
                list-style: none;
                height: 24px;
                line-height: 22px;
            }

                .zdyUL li a {
                    cursor: pointer;
                    padding: 5px 10px;
                    border-radius: 4px;
                }

                .zdyUL li i {
                    margin-right: 4px;
                    font-size: 13px;
                    color: #666;
                    vertical-align: middle;
                    margin-top: -1px;
                }

                .zdyUL li a:hover {
                    text-decoration: none;
                    background-color: #1ABC9C;
                    color: #fff;
                }
    </style>
}
<div class="topPanel">
    <div class="toolbar">
        <div class="btn-group">
            <a class="btn btn-primary" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span></a>
        </div>
    </div>
    <div class="search">
        <table>
            <tr>
                <td>
                    <div class="input-group">
                        <input id="txt_keyword" type="text" class="form-control" placeholder="请输入要查询活动名称" style="width: 200px;">
                        <span class="input-group-btn">
                            <button id="btn_search" type="button" class="btn  btn-primary"><i class="fa fa-search"></i></button>
                        </span>
                    </div>
                    <div id="search_orderState" class="btn-group">
                        <a class="btn btn-default active" data-value="0">待收付</a>
                        <a class="btn btn-default" data-value="3">销售已确认收款</a>
                        <a class="btn btn-default" data-value="1">财务已确认收款</a>
                        <a class="btn btn-default" data-value="2">财务已确认付款</a>
                        <a class="btn btn-default" data-value="4">已退款</a>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</div>

<div class="gridPanel">
    <table id="gridList"></table>
    <div id="gridPager"></div>
</div>

<div id="Invoicesssss" style="display:none">
    <div style="padding-top: 5px;">
        <table class="form ui-jqgrid-htable ui-common-table table table-bordered">
            <tr class="ui-jqgrid-labels">
                <td class="formTitle">发票号</td>
                <td class="formValue">
                    <input id="F_InvoiceNo" name="F_InvoiceNo" type="number" class="form-control" placeholder="多个发票号用“/”隔开" />
                </td>
            </tr>
            <tr>
                <td>
                    <input type="hidden" id="rowid" name="name" value="" />
                </td>
                <td>
                    <button type="button" class="btn btn-primary" onclick="btn_UpdateStates()">确认收款</button>
                </td>
            </tr>
        </table>
    </div>
    <script>

        function btn_UpdateStates() {
            if ($("#F_InvoiceNo").val().length>1) {
                $.ajax({
                    url: "/OrderFlowManage/ClientOrder/UpdateCashState",
                    data: { keyValue: $("#rowid").val(), state: 1, Ino: $("#F_InvoiceNo").val()},
                    dataType: "json",
                    async: false,
                    success: function (data) {
                        if (data.state == "success") {
                            $.modalAlert("确认收款成功!", "warning");
                        } else {
                            $.modalAlert(data.message, "warning");
                            $.currentWindow().$("#gridList").trigger("reloadGrid");
                        }
                    }
                });
            }
            else {
                $.modalAlert("请先填写发票号后再提交数据!", "warning");
            }
        }

    </script>
</div>

    <script>
        // from page type
        var pageType = $.request("type");
        var citys = [],
            OrderTodos = {};

        $(function () {
            $("#search_orderState a.btn-default").click(function () {
                $("#search_orderState a.btn-default").removeClass("active");
                $(this).addClass("active");
                $('#btn_search').trigger("click");
            });

            $.ajax({
                url: "/OrderFlowManage/ClientOrder/GetCitysJson",
                data: {},
                dataType: "json",
                async: false,
                success: function (data) {
                    citys = data;
                    gridList();
                }
            });
        })

        function btn_Invoice(rowId, state) {
            var opts = { btn: ['是', '否'] };
            $.modalConfirm("确认该笔款项已实收到账？", function (flag, index) {
                if (flag) {
                    $("#rowid").val(rowId);
                    $.modalOpen({
                        id: "InvoiceFrom",
                        type: 1,
                        title: "发票信息",
                        url: $("#Invoicesssss").html(),
                        width: "500px",
                        height: "300px",
                        btn: [],
                        callBack: function (iframeId) {
                        }
                    });
                }
            }, opts); 
        }
        function gridList() {
            var $gridList = $("#gridList");
            var stateArr = ['待收付', '财务确认收款', '财务确认付款', '销售确认收款', '已退款'];
            var AType = ['收款', '付款', '预定金', '押金'];

            var queryJson = {
                keyword: $("#txt_keyword").val(),
                orderstate: $("#search_orderState a.active").attr('data-value'),
                type: pageType
            };
            $gridList.dataGrid({
                url: "/OrderFlowManage/ClientOrder/GetOrderCashGridJson",
                height: $(window).height() - 176,
                colModel: [
                    {
                        label: '操作', name: 'F_Id', width: 180, align: 'center', sortable: false,
                        formatter: function (cellvalue, options, rowObject) {
                            var html = '<div class="btn-group btn-group-xs" style="margin-top:5px;">';
                            //收款\预付金\押金
                            if (rowObject.F_InOutType != "1") {
                                // 状态为待收付款 需要销售确认收款
                                if (rowObject.F_State == "0" && pageType == "1") {
                                    html += '<button type="button" class="btn btn-link" onclick="btn_UpdateState(3,\'' + rowObject.F_Id + '\')">确认收款</button>';
                                }
                                // 状态为销售确认收款需财务确认收款
                                if (rowObject.F_State == "3" && pageType == "2") {
                                    html += '<button type="button" class="btn btn-link" onclick="btn_Invoice(\'' + rowObject.F_Id + '\')">确认收款</button>';
                                }
                            }//付款状态，待财务确认付款
                            else if (rowObject.F_InOutType == "1" && pageType == "2" && rowObject.F_State == "0") {
                                html += '<button type="button" class="btn btn-link" onclick="btn_UpdateState(2,\'' + rowObject.F_Id + '\')">确认付款</button>';

                            }
                            if (rowObject.F_InOutType == "3" && pageType == "2" && rowObject.F_State != "4") {
                                html += '<button type="button" class="btn btn-link" onclick="btn_UpdateState(4,\'' + rowObject.F_Id + '\')">退款</button>';
                            }
                            html += '</div>';

                            return html;
                        }
                    },
                    {
                        label: '活动城市', name: 'F_ActivityCityId', width: 100, sortable: false, align: 'left',
                        formatter: function (cellvalue, options, rowObject) {
                            if (cellvalue) {
                                var arr = cellvalue.indexOf(',') == -1 ? [cellvalue] : cellvalue.split(',');
                                var result = [];
                                for (var i = 0; i < citys.length; i++) {
                                    var ele = citys[i];
                                    for (var j = 0; j < arr.length; j++) {
                                        var val = arr[j];
                                        if (val == ele.Value) {
                                            result.push(ele.Text);
                                        }
                                    }
                                }
                                return result.toString();
                            }
                            return '';
                        }
                    },
                    {
                        label: '活动日期', name: 'F_ActivityTime', width: 150, align: 'center', sortable: false
                    },
                    { label: '活动主题', name: 'F_ActivityTheme', width: 200, sortable: false, align: 'left' },
                    {
                        label: '应收/付款金额', name: 'F_InOutCash', width: 80, sortable: false, align: 'left',
                        formatter: function (cellvalue, options, rowObject) {
                            if (rowObject.F_InOutType == 1)
                                return '-' + cellvalue;
                            else
                                return cellvalue
                        }
                    },
                    {
                        label: '应收/付款时间', name: 'F_InOutDate', width: 80, align: 'center',
                        formatter: "date", formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' }
                    },
                    { label: '实收/付款金额', name: 'F_ActualAmount', width: 120, sortable: false, align: 'left' },
                    {
                        label: '实收/付款时间', name: 'F_ActualCollection', width: 100, align: 'center',
                        formatter: "date", formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' }
                    },
                    {
                        label: '收支类型', name: 'F_InOutType', width: 80, align: 'left',
                        formatter: function (cellvalue, options, rowObject) {
                            return AType[cellvalue] || '';
                        }
                    },
                    {
                        label: '状态', name: 'F_State', width: 80, align: 'left',
                        formatter: function (cellvalue, options, rowObject) {
                            if (cellvalue == 0) {
                                if (rowObject.F_InOutType == "1") {
                                    return "<span style='color:red;'>待付款</span>";
                                } else {
                                    return "<span style='color:green;'>待收款</span>";
                                }
                            }
                            else {
                                return stateArr[cellvalue] || '';
                            }
                        }
                    },
                    { label: '活动编号', name: 'spid', hidden: true },
                    { label: '编号', name: 'F_Id', width: 250, hidden: true }
                ],
                postData: {
                    queryJson: JSON.stringify(queryJson)
                },
                pager: "#gridPager",
                sortname: 'F_OrderId',
                viewrecords: true,
                ondblClickRow: function (rowid, iRow, iCol, e) {
                    var rowobj = $gridList.getRowData(rowid);
                    btn_statedetail(rowobj["F_Id"], flowStateArr.indexOf(rowobj["F_OrderState"]))
                }
            });
            if (pageType == 2) {
                jQuery("#setGridParam").setGridParam().hideCol("F_Id").trigger("reloadGrid");
            }
            $("#btn_search").click(function () {
                var queryJsons = {
                    keyword: $("#txt_keyword").val(),
                    orderstate: $("#search_orderState a.active").attr('data-value'),
                    type: pageType
                };
                $gridList.jqGrid('setGridParam', {
                    postData: { queryJson: JSON.stringify(queryJsons) },
                }).trigger('reloadGrid');
            });
        }
        var opts = { btn: ['是', '否'] };
        function btn_UpdateState(value, rowid) {
            var textmsg = "";
            if (value == 3) {
                textmsg = "确认该笔款项已收到？";
            } else if (value == 2) {
                textmsg = "确认该笔款项已付款成功？";
            }
            else if (value == 4) {
                textmsg = "确认该笔款项已退款成功？";
            }
            $.modalConfirm(textmsg, function (flag, index) {
                if (flag) {
                    $.ajax({
                        url: "/OrderFlowManage/ClientOrder/UpdateCashState",
                        data: { keyValue: rowid, state: value, Ino: "" },
                        dataType: "json",
                        async: false,
                        success: function (data) {
                            if (data.state == "success") {
                                $.modalAlert("操作成功!", "warning");
                            } else {
                                $.modalAlert(data.message, "warning");
                                $.currentWindow().$("#gridList").trigger("reloadGrid");
                            }
                        }
                    });
                }
            }, opts);
        }

    </script>
